package com.lgx.right.controller;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgx.common.exception.DataException;
import com.lgx.common.web.json.JsonUtils;
import com.lgx.common.web.util.EscapeUtils;
import com.lgx.right.entity.Permission;
import com.lgx.right.entity.Resource;
import com.lgx.right.service.IPermissionService;
import com.lgx.right.service.IResourceService;

@Controller
@RequestMapping("/resource")
public class ResourceController {

	@Autowired
	private IResourceService resourceService;
	
	@Autowired
	private IPermissionService permissionService;

	public IResourceService getResourceService() {
		return resourceService;
	}

	public void setResourceService(IResourceService resourceService) {
		this.resourceService = resourceService;
	}
	
	public IPermissionService getPermissionService() {
		return permissionService;
	}

	public void setPermissionService(IPermissionService permissionService) {
		this.permissionService = permissionService;
	}

	@RequestMapping("/resourceMgr.do")
	public String resourceMgr(HttpServletRequest request,HttpServletResponse response)
	{
		return "right/resource/resourceMgr";
	}
	
	@RequestMapping("/list.do")
	public void list(HttpServletRequest request,HttpServletResponse response)
	{
		List<Resource> ps = resourceService.findResourceList();
		JsonUtils utils = new JsonUtils();
		String json;
		try {
			json = utils.converToJson(ps);
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.print(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/save.do")
	@ResponseBody
	public String save(HttpServletRequest request,HttpServletResponse response)
	{
		String resourceId = request.getParameter("resourceId");
		Resource parent = this.resourceService.findResourceById(Long.parseLong(resourceId));
		String permissionId = request.getParameter("permissionId");
		Permission permission = this.permissionService.findPermissionById(Long.parseLong(permissionId));
		String name = request.getParameter("name");
		if(!StringUtils.isEmpty(name))name = EscapeUtils.unescape(name);
		String url = request.getParameter("url");
		
		Resource r = new Resource();
		r.setName(name);
		r.setParent(parent);
		r.setUrl(url);
		r.setPermission(permission);
		r.setIsDelete("0");
		int level = Integer.parseInt(parent.getLevel())+1;
		r.setLevel(level+"");
		
		try {
			this.resourceService.createResource(r);
		} catch (DataException e1) {
			e1.printStackTrace();
		}
		
		request.setAttribute("message", "操作成功！");
		return "1";
	}
	
	@RequestMapping("/update.do")
	@ResponseBody
	public String update(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException
	{
		String resourceId = request.getParameter("resourceId");
		Resource r = this.resourceService.findResourceById(Long.parseLong(resourceId));
		
		String name = request.getParameter("name");
		if(!StringUtils.isEmpty(name))name = EscapeUtils.unescape(name);
		String url = request.getParameter("url");
		String layerOrder = request.getParameter("layerOrder");
		
		r.setName(name);
		r.setUrl(url);
		r.setLayerOrder(layerOrder);
		
		try {
			this.resourceService.updateResource(r);
		} catch (DataException e1) {
			e1.printStackTrace();
		}
		
		request.setAttribute("message", "操作成功！");
		return "1";
	}
	
	@RequestMapping("/delete.do")
	@ResponseBody
	public String delete(HttpServletRequest request,HttpServletResponse response)
	{
		String resourceId = request.getParameter("resourceId");
		try {
			this.resourceService.deleteResource(Long.parseLong(resourceId));
			return "1";
		} catch (DataException e1) {
			e1.printStackTrace();
		}
		return "0";
	}
}
